package com.epfresh.supplier.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.OrderAgentList;

import java.util.List;

/**
 * Created by zzq on 2016/7/19.
 */
public class OrderAgentAdapter extends BaseAdapter implements View.OnClickListener {

    public ListView lv;
    public Context context;
    public List<OrderAgentList> list;
    int type;
    private AdapterView.OnItemClickListener onItemClickListener;

    public AdapterView.OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OrderAgentAdapter(Context context, ListView lv, List<OrderAgentList> groupList) {
        this.context = context;
        this.list = groupList;
        this.lv = lv;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int groupPosition, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_list_order_agent, null, false);
            vh.ll_item_order_agent = convertView.findViewById(R.id.ll_item_order_agent);
            vh.tv_item_order_agent_id = (TextView) convertView.findViewById(R.id.tv_item_order_agent_id);
            vh.tv_item_order_agent_from = (TextView) convertView.findViewById(R.id.tv_item_order_agent_from);
            vh.tv_item_order_agent_to = (TextView) convertView.findViewById(R.id.tv_item_order_agent_to);
            vh.tv_item_order_agent_state = (TextView) convertView.findViewById(R.id.tv_item_order_agent_state);
            vh.tv_item_order_agent_date = (TextView) convertView.findViewById(R.id.tv_item_order_agent_date);

            vh.iv_item_order_agent_product = (ImageView) convertView.findViewById(R.id.iv_item_order_agent_product);
            vh.tv_item_order_agent_product_title = (TextView) convertView.findViewById(R.id.tv_item_order_agent_product_title);
            vh.tv_item_order_agent_product_price = (TextView) convertView.findViewById(R.id.tv_item_order_agent_product_price);
            vh.tv_item_order_agent_product_size_detail = (TextView) convertView.findViewById(R.id.tv_item_order_agent_product_size_detail);
            //vh.tv_item_order_agent_product_size_already = (TextView) convertView.findViewById(R.id.tv_item_order_agent_product_size_already);

            vh.tv_item_order_agent_money_percent = (TextView) convertView.findViewById(R.id.tv_item_order_agent_money_percent);
            vh.tv_item_order_agent_money_all = (TextView) convertView.findViewById(R.id.tv_item_order_agent_money_all);
            vh.tv_item_order_agent_goods_code = (TextView) convertView.findViewById(R.id.tv_item_order_agent_goods_code);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        vh.tv_item_order_agent_goods_code.setTag(R.id.item_key_position, groupPosition);
        vh.tv_item_order_agent_goods_code.setOnClickListener(this);

        vh.ll_item_order_agent.setTag(R.id.item_key_position, groupPosition);
        vh.ll_item_order_agent.setOnClickListener(this);

        OrderAgentList order = list.get(groupPosition);
        vh.tv_item_order_agent_id.setText("订单："+order.getNumber());
        vh.tv_item_order_agent_from.setText(order.getBaseCityName());
        vh.tv_item_order_agent_to.setText(order.getAgencyCityName());
        vh.tv_item_order_agent_date.setText("预计"+order.getDeliverTime()+"发货");
        ImageLoader.getInstance().displayImage(order.getThumbnail(), vh.iv_item_order_agent_product);

        vh.tv_item_order_agent_product_title.setText(order.getProductTitle());
        vh.tv_item_order_agent_product_price.setText(order.getPriceUnitRange());
        vh.tv_item_order_agent_product_size_detail.setText("总集货量："+order.getSumProductCount()+order.getChargeUnit());

        vh.tv_item_order_agent_money_all.setText(order.getSumReqProductPrice()+"元");
        vh.tv_item_order_agent_money_percent.setText("代办费"+order.getAgencyExpenseRate()+"%："+order.getAgencySettleAmount()+"元");
        vh.tv_item_order_agent_state.setText(order.getGrouponStatusTitle());
        if(order.getButtonList()!=null&&order.getButtonList().size()>0){
            vh.tv_item_order_agent_goods_code.setVisibility(View.VISIBLE);
        }else {
            vh.tv_item_order_agent_goods_code.setVisibility(View.GONE);
        }
        if(order.getAgencySettleStatus()==20||order.getAgencySettleStatus()==30){
            vh.tv_item_order_agent_money_percent.setVisibility(View.VISIBLE);
        }else {
            vh.tv_item_order_agent_money_percent.setVisibility(View.GONE);
        }
/*
        vh.tv_item_order_agent_date.setText(order.getPlaceOrderTime());
        vh.tv_item_order_agent_to.setText(order.getReceiverPhone());
        vh.tv_item_order_agent_state.setText(order.getPurchaseStatusTitle());////
*/
        if(order.getAgencySettleStatus() == 20||order.getAgencySettleStatus() == 30){//结算中调整title
            vh.tv_item_order_agent_state.setText(order.getAgencySettleStatusTitle());
        }

        convertView.setTag(vh);
        convertView.setClickable(true);
        return convertView;
    }

    public class ViewHolder {
        View ll_item_order_agent;

        TextView tv_item_order_agent_id;

        TextView tv_item_order_agent_from, tv_item_order_agent_to;
        TextView tv_item_order_agent_state;
        TextView tv_item_order_agent_date;

        ImageView iv_item_order_agent_product;
        TextView tv_item_order_agent_product_title;
        TextView tv_item_order_agent_product_price;
        TextView tv_item_order_agent_product_size_detail;
        //TextView tv_item_order_agent_product_size_already;

        TextView tv_item_order_agent_money_percent;
        TextView tv_item_order_agent_money_all;
        TextView tv_item_order_agent_goods_code;
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            int position = (int) tag;
// 交货码，查看物流，取消订单，确认收货，支付， 详情
            onItemClickListener.onItemClick(null,v,position,position);



        }
    }
}
